﻿using System;
using System.Windows.Forms;

namespace dgmobile
{
  public partial class Form1 : Form
  {
    //For updating, what say you?
    private static Timer UpdateClock;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      //OFFLINE TESTS
      if(Config.TestMode)
      {
        return;
      }

      //WARNING! WARNING!
      MessageBox.Show("Conexão inicial com banco de dados pode demorar alguns segundos. Favor aguardar tela principal ou mensagem definitiva de erro.\n\n\rApós conexão, informe uma chapa válida para poder inserir dados.", "Aviso");

      //Checking if connection to DB is ok. Dies if not.
      if(!DB.Connect())
      {
        MessageBox.Show("Não há conectividade com o banco de dados. Verifique sua conexão ou entre em contato com DGNOF. O programa será encerrado.", "Erro");
        this.Close();
        Application.Exit();
        Environment.Exit(0);
      }

      //First grid setup
      FillGrid(this, null);

      //Defaults user to no one (000000).
      ChangeUser(textBoxChapa.Text);

      //Starts clock for update.
      UpdateClock = new Timer();
      UpdateClock.Interval = 3600000; //1 fucking hour!
      UpdateClock.Tick += UpdateCheck;
      UpdateClock.Start();
    }

    //Accepts <Enter> in textbox to validate.
    private void textBoxChapa_KeyPress(object sender, KeyPressEventArgs e)
    {
      if(e.KeyChar == Convert.ToChar(Keys.Enter))
      {
        ChangeUser(textBoxChapa.Text);
      }
    }

    private void buttonChangeUser_Click(object sender, EventArgs e)
    {
      ChangeUser(textBoxChapa.Text);
    }

    private void buttonAddAtendimento_Click(object sender, EventArgs e)
    {
      Popup(0);
    }

    private void buttonAddMelhoria_Click(object sender, EventArgs e)
    {
      Popup(1);
    }

    private void buttonAddBD_Click(object sender, EventArgs e)
    {
      Popup(2);
    }

    private void buttonAddOS_Click(object sender, EventArgs e)
    {
      Popup(3);
    }

    //Accepts RIGHT CLICK on dataGridView and summons context menu on it.
    private void dataGridView_MouseDown(object sender, MouseEventArgs e)
    {
      if(e.Button == System.Windows.Forms.MouseButtons.Right)
      {
        var test = dataGridView.HitTest(e.X, e.Y);
        if(test.RowIndex >= 0)
        {
          dataGridView.ClearSelection();
          dataGridView.Rows[test.RowIndex].Selected = true;
          contextMenuStrip1.Show(MousePosition);
        }
      }
    }

    //Enabling data deletion, only through context menu.
    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
      int id = (Int32)dataGridView.SelectedRows[0].Cells[0].Value;
      DB.DeleteData(id);
      FillGrid(this, null);
    }

    //Accepts keyboard commands/shortcuts on form!
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
      //DENIED! if not logged in.
      if(Config.Operator == null)
      {
        return;
      }

      //Atendimento.
      if((e.Control) && (e.KeyCode == Keys.A))
      {
        Popup(0);
      }

      //Melhoria.
      if((e.Control) && (e.KeyCode == Keys.M))
      {
        Popup(1);
      }

      //BD.
      if((e.Control) && (e.KeyCode == Keys.B))
      {
        Popup(2);
      }

      //OS.
      if((e.Control) && (e.KeyCode == Keys.O))
      {
        Popup(3);
      }

      //Ajuda.
      if(e.KeyCode == Keys.F1)
      {
        buttonHelpClick(this, e);
      }

      //Atualizar.
      if(e.KeyCode == Keys.F5)
      {
        buttonRefreshClick(this, e);
      }
    }

    private void buttonRefreshClick(object sender, EventArgs e)
    {
      FillGrid(this, null);
    }

    private void buttonHelpClick(object sender, EventArgs e)
    {
      MessageBox.Show("Arquivo de ajuda ainda não disponível. Contato com valeriano@outlook.com ou (22) 2533-2984.");
    }

    private void buttonXL_Click(object sender, EventArgs e)
    {
      XL.MonthlyReport();
    }
  }
}